#include "config.h"

#include "ms2sim.h"

ecm_uint64	ts_subsys;

typedef struct {
	ecm_uint64 (*probe)(ecm_uint64 ts);
	void (*schedule)(ecm_uint64 ts);
} subsys_info_t;

static subsys_info_t	subsys_infos[] = {
	{ probe_stat_system, schedule_stat_system },
	{ probe_request_system, schedule_request_system },
	{ probe_service_system, schedule_service_system },
	{ probe_disk_system, schedule_disk_system },
	{ probe_network_system, schedule_network_system },
};

void
run_sub_systems(void)
{
	ecm_uint64	ts_max = (ecm_uint64)duration * USEC_MAX;
	ecm_uint32	i;

	for (ts_subsys = 0; !going_to_shutdown && ts_subsys < ts_max;) {
		subsys_type_t	type = SUBSYS_TYPE_STAT + 1;
		ecm_uint64	ts_min;
		ecm_uint64	ts_stat;

		ts_min = subsys_infos[type - 1].probe(ts_subsys);
		for (i = type + 1; i <= SUBSYS_TYPE_MAX; i++) {
			ecm_uint64	ts_probed = subsys_infos[i - 1].probe(ts_subsys);
			if (ts_probed < ts_min) {
				type = (subsys_type_t)i;
				ts_min = ts_probed;
			}
		}

		for (ts_stat = subsys_infos[0].probe(ts_subsys);
		     ts_stat < ts_min && !going_to_shutdown;
		     ts_stat = subsys_infos[0].probe(ts_stat)) {
			subsys_infos[0].schedule(ts_stat);
		}

		if (going_to_shutdown)
			break;

		ts_subsys = ts_min;
		subsys_infos[type - 1].schedule(ts_subsys);
	}
}

